package cn.broadmax.boot.modules.sys.service.impl;

import cn.broadmax.boot.modules.sys.entity.SysRole;
import cn.broadmax.boot.modules.sys.entity.SysRoleResource;
import cn.broadmax.boot.modules.sys.entity.SysUserRole;
import cn.broadmax.boot.modules.sys.service.ISysRoleResourceService;
import cn.broadmax.boot.modules.sys.service.ISysRoleService;
import cn.broadmax.boot.modules.sys.service.ISysUserRoleService;
import cn.broadmax.boot.modules.sys.bean.role.RoleCreateRequest;
import cn.broadmax.boot.modules.sys.bean.role.RoleUpdateRequest;
import cn.broadmax.boot.modules.sys.mapper.SysRoleMapper;
import cn.broadmax.boot.util.SAssert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jexx.util.CollectionUtil;
import jexx.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author jexx
 * @since 2020-07-21
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private final ISysRoleResourceService sysRoleResourceService;
    private final ISysUserRoleService sysUserRoleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysRole addRole(RoleCreateRequest request) {
        SAssert.hasText(request.getName(), "角色名称不能为空");
        SAssert.hasText(request.getCode(), "角色code不能为空");
        SysRole existRole = baseMapper.getByCodeSysRole(request.getCode());
        SAssert.isNull(existRole, "角色code已存在");

        SysRole role = new SysRole();
        role.setName(request.getName());
        role.setCode(request.getCode());
        role.setDescription(request.getDescription());
        super.save(role);

        if(CollectionUtil.isNotEmpty(request.getResourceIds())){
            List<SysRoleResource> roleResources = new ArrayList<>();
            for(String resourceId : request.getResourceIds()){
                SysRoleResource roleResource = new SysRoleResource();
                roleResource.setRoleId(role.getId());
                roleResource.setResourceId(resourceId);
                roleResources.add(roleResource);
            }
            sysRoleResourceService.saveBatch(roleResources);
        }
        return getById(role.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysRole updateRoleById(String roleId, RoleUpdateRequest request) {
        SAssert.hasText(request.getName(), "角色名称不能为空");

        SysRole update = new SysRole();
        update.setId(roleId);
        update.setName(request.getName());
        update.setDescription(request.getDescription());
        boolean success = super.updateById(update);
        if(success){
            sysRoleResourceService.updateResourcesByRoleId(roleId, request.getResourceIds());
        }
        return getById(roleId);
    }

    @Override
    public List<SysRole> getRolesByUserId(String userId) {
        if(StringUtil.isEmpty(userId)){
            return Collections.emptyList();
        }

        List<SysUserRole> userRoles = sysUserRoleService.getUserRolesByUserId(userId);
        if(CollectionUtil.isEmpty(userRoles)){
            return Collections.emptyList();
        }
        List<String> roleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        return super.listByIds(roleIds);
    }

}
